package com.cll.dao_ru_demo.utils;

import excel.ExcelUtils;
import org.apache.poi.ss.usermodel.Workbook;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class Poi {

    //必须先赋值
    //1.传入集合
    private List<Object> list;
    //2.表标题
    private String title;
    //3.表头
    private String[] headers;
    //4.表内容
    private Object[][] fields;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }



    //5.获取规则
    public  ExcelUtils.ExportRules getRols(){

        return  new ExcelUtils.ExportRules(false,this.fields,this.title,this.headers,null);
    }
    //6.获取文件
    public   Workbook getWorkbook() throws  Exception{

        return new ExcelUtils().createWorkbook(this.list,getRols());
    }
    //7.设置响应参数
    public static void getResponse(HttpServletResponse response) throws  Exception{
        response.reset();
        response.setContentType("application/vnd.ms-excel:charset=utf-8");
        //注意符号问题很重要 很重要 很重要 重要的事情说三遍 看好了！！！！！
        response.setHeader("Content-Disposition","attachment;filename="+new String(("测试.xlsx").getBytes(),"iso-8859-1"));
    }
    //8.输出到前台
    public  void write(HttpServletResponse response) throws  Exception{

      getWorkbook().write(response.getOutputStream());
    }

    public List<Object> getList() {
        return list;
    }

    public void setList(List<Object> list) {
        this.list = list;
    }

    public String[] getHeaders() {
        return headers;
    }

    public void setHeaders(String[] headers) {
        this.headers = headers;
    }

    public Object[][] getFields() {
        return fields;
    }

    public void setFields(Object[][] fields) {
        this.fields = fields;
    }



}
